package Controller;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

import Entity.ComplaintReport;
import Entity.DisabledPlace;
import Entity.DisabledReport;
import Entity.FullSubscription;
import Entity.OneTimeCostumerOrder;
import Entity.OrderReport;
import Entity.Packet;
import Entity.Packet.Kind;
import client.ChatClient;

public class GoodStaff 
{
	private static ChatClient client;
	private static Packet input;

	public static void saveOrderReport(OrderReport r) 
	{
		String normal="insert into OrderReport values("+r.getParkingLot()+","+r.getStart().getTime()+","+r.getEndDate().getTime()+",\""+r.getDescription()+"\","+r.getNumOfOrders()+","+r.getNumOfInAdvance()+","+r.getNumOfSubs()+");";
		input=null;
		Packet p1=new Packet();
		p1.setE(Kind.save);
		p1.setO(normal);
		try {
			client.sendToServer((Object)p1);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}

	public static void saveComplaintReport(ComplaintReport r) 
	{
		String normal="insert into ComlaintReport values("+r.getParkingLot()+","+r.getStart().getTime()+","+r.getEndDate().getTime()+",\""+r.getDescription()+"\");";
		input=null;
		Packet p1=new Packet();
		p1.setE(Kind.save);
		p1.setO(normal);
		try {
			client.sendToServer((Object)p1);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}

	public static void saveDisabledReport(DisabledReport r) 
	{
		String normal="insert into DisabledReport values("+r.getParkingLot()+","+r.getStart().getTime()+","+r.getEndDate().getTime()+",\""+r.getDescription()+"\");";
		input=null;
		Packet p1=new Packet();
		p1.setE(Kind.save);
		p1.setO(normal);
		try {
			client.sendToServer((Object)p1);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}

	public static OneTimeCostumerOrder[] getOneTimeCustomerOrder() 
	{
		int count=0;
		input=null;
		Packet p=new Packet();
		p.setE(Kind.get);
		String query="SELECT * FROM test.OneTimeCustomerOrder;";
		p.setO(query);
		try {
			client.sendToServer((Object)p);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		while (input==null);
		ResultSet rs=(ResultSet)input.getO();
		try {
			while(rs.next())
			{
				count++;
			}
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} 
		try {
			rs.beforeFirst();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		OneTimeCostumerOrder [] c=new OneTimeCostumerOrder[count];
		int i=0;
		try {
			while(rs.next())
			{
				Date leave=new Date(rs.getLong(2)),enter=new Date(rs.getLong(3)),actual=new Date(rs.getLong(4));
				
				c[i]=new OneTimeCostumerOrder(rs.getString(1),leave,rs.getString(5),rs.getString(6));
				c[i].setActual_leaving_hour(actual);
				c[i].setEntering_hour(enter);
				c[i].setPayment_status(rs.getBoolean(7));
				i++;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		return c;
	}

	public static DisabledPlace getCurrectDisabledParkingSpot(int[] place,int parkingLot) 
	{
		int count=0;
		input=null;
		Packet p=new Packet();
		p.setE(Kind.get);
		String query="SELECT * FROM test.DisabledPlace WHERE DisabledPlace.ParkingLot"+parkingLot+" AND DisabledPlace.Row="+place[0]+" AND DisabledPlace.Col="+place[1]+" AND DisabledPlace.Depth="+place[2]+" AND DisabledPlace.End IS NULL;";
		p.setO(query);
		try {
			client.sendToServer((Object)p);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		while (input==null);
		ResultSet rs=(ResultSet)input.getO();
		DisabledPlace c = null;
		try {
			rs.next();
			Date start=new Date(rs.getLong(7)), end=new Date(rs.getLong(8));
			int [] plac={rs.getInt(3),rs.getInt(4),rs.getInt(5)};
			c=new DisabledPlace(rs.getInt(2),plac,rs.getString(6));
			c.setPid(rs.getInt(1));
			c.setStart(start);
			c.setEnd(end);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		return c;
	}
	
	public void saveFullSub(FullSubscription p)
	{
		String normal="insert into DisabledReport values(\""+p.getSubscription_number()+"\",\""+p.getCustomer_id()+"\",\""+p.getCar_number()+"\","+p.getStart_date().getTime()+","+p.getEnd_date().getTime()+");";
		input=null;
		Packet p1=new Packet();
		p1.setE(Kind.save);
		p1.setO(normal);
		try {
			client.sendToServer((Object)p1);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}
}
